<?php
namespace app\back\controller;


use app\back\model\ConfigModel;
use app\back\model\NodeTemplateModel;
use think\Request;

class Template extends AdminCommon
{


    ///已有模板展示列表
    public function templateList(ConfigModel $configModel){
        $this->assign([
            'indexDataList'=>$configModel->getIndexDataListByPosition('template_config'),
        ]);
        return view();
    }



    ///网站关联模板管理列表页
    public function template(NodeTemplateModel $nodeTemplateModel){
        $this->assign([
            'config_data'=>$nodeTemplateModel->getIndexDataList(),
        ]);
        return view();
    }


    ///网站关联模板添加页面
    public function templateAdd(){
        $this->assignTemplateConfigData();
        return view();
    }
    ///网站关联模板添加操作
    public function templateAddPost(Request $request,NodeTemplateModel $nodeTemplateModel){
        $node_title = $request->param('node_title','');
        $node_url = $request->param('node_url','','trim');
        $template_data_arr = explode('+',$request->param('template_data',''));

        ///参数效验
        if(empty($node_title)){return think_json_encode(['status'=>2,'msg'=>'网站标题不能为空啊！']);}
        if(empty($template_data_arr)){return think_json_encode(['status'=>2,'msg'=>'使用模板不能为空啊！']);}
        if(!cusCheckUrl($node_url)){return think_json_encode(['status'=>2,'msg'=>'检查一下网站URL吧！']);}

        list($template_config_id,$template_config_path) = $template_data_arr;

        $check_res = $nodeTemplateModel->addCheckUrlItExist($node_url);
        if($check_res > 0){return think_json_encode(['status'=>2,'msg'=>'此网址已经配置了模板！']);}

        /// 启动事务
        $nodeTemplateModel->startTrans();
        try{
            $add_res = $nodeTemplateModel->addNewTemplateData($node_title,$node_url,$template_config_id,$template_config_path);
            if($add_res == 1){
                $nodeTemplateModel->resetTemplateFile();
                /// 提交事务
                $nodeTemplateModel->commit();
                return think_json_encode(['status'=>0,'msg'=>'操作成功！']);
            }
            /// 回滚事务
            $nodeTemplateModel->rollback();
            return think_json_encode(['status'=>2,'msg'=>'出现错误，刷新页面试试！']);
        }catch(\Exception $e){
            /// 回滚事务
            $nodeTemplateModel->rollback();
            trace($e->getMessage(),'error');
            return think_json_encode(['status'=>2,'msg'=>'新增出现异常，请检查啊！']);
        }
    }

    ///网站关联模板修改页面
    public function templateEdit(Request $request,NodeTemplateModel $nodeTemplateModel){
        $n_id = $request->param("id",0,'intval');
        ///当前操作数据
        $nodeTemplateData = $nodeTemplateModel->getDataById($n_id);


        $this->assignTemplateConfigData();
        $this->assign([
            'nodeTemplateData'=>$nodeTemplateData,
        ]);
        return view();
    }
    ///网站关联模板修改操作
    public function templateEditPost(Request $request,NodeTemplateModel $nodeTemplateModel){
        $node_template_id = $request->param('node_template_id',0,'intval');
        $node_title = $request->param('node_title','');
        $node_url = $request->param('node_url','','trim');
        $template_data_arr = explode('+',$request->param('template_data',''));

        ///参数效验
        if(empty($node_template_id)){return think_json_encode(['status'=>2,'msg'=>'页面存在错误，请刷新！']);}
        if(empty($node_title)){return think_json_encode(['status'=>2,'msg'=>'网站标题不能为空啊！']);}
        if(empty($template_data_arr)){return think_json_encode(['status'=>2,'msg'=>'使用模板不能为空啊！']);}
        if(!cusCheckUrl($node_url)){return think_json_encode(['status'=>2,'msg'=>'检查一下网站URL吧！']);}

        list($template_config_id,$template_config_path) = $template_data_arr;

        $check_res = $nodeTemplateModel->editCheckUrlItExist($node_url,$node_template_id);
        if($check_res > 0){return think_json_encode(['status'=>2,'msg'=>'此网址已经配置了模板！']);}
        /// 启动事务
        $nodeTemplateModel->startTrans();
        try{
            $update_res = $nodeTemplateModel->saveUseTemplate($node_title,$node_url,$template_config_id,$template_config_path,$node_template_id);
            if($update_res == 1){
                $nodeTemplateModel->resetTemplateFile();
                /// 提交事务
                $nodeTemplateModel->commit();
                return think_json_encode(['status'=>0,'msg'=>'操作成功！']);
            }
            /// 回滚事务
            $nodeTemplateModel->rollback();
            return think_json_encode(['status'=>2,'msg'=>'出现错误，刷新页面试试！']);
        }catch(\Exception $e){
            /// 回滚事务
            $nodeTemplateModel->rollback();
            trace($e->getMessage(),'error');
            return think_json_encode(['status'=>2,'msg'=>$e->getMessage()]);
        }
    }



    ///赋值已有模板数据
    private function assignTemplateConfigData(){
        $configModel = new ConfigModel();
        ///已有的模板数据
        $templateConfigData = $configModel->getTemplateConfigDataList();
        $this->assign([
            'templateConfigData'=>$templateConfigData,
        ]);
    }


}